home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 June: Reference Library / Dev.CD Jun 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 28 / develop issue 28 code / sketch / source / appleevent / appleevent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-26  |  6.1 KB  |  239 lines

  1. /****************************************************************************
  2.  * 
  3.  * AppleEvent.c
  4.  * 
  5.  * Functions related to processing AppleEvents and dispatching to the appropriate handlers
  6.  *
  7.  ****************************************************************************/
  8.  
  9. #include <string.h>
  10. #include "StringUtils.h"
  11.  
  12. #include "AppleEvent.h"
  13.  
  14. #include "AERRequiredSuite.h"
  15. #include "AERCoreSuite.h"
  16. #include "AERMiscSuite.h"
  17. #include "OSLHelpers.h"
  18.  
  19.  
  20. /*****************************************************************************
  21.  * 
  22.  * InstallAEHandlers
  23.  * 
  24.  * Call this at program initialization time
  25.  * 
  26.  *****************************************************************************/
  27.  
  28. Boolean 
  29. InstallAEHandlers()
  30. {    
  31.     Boolean success = HasAppleEvents();
  32.         
  33.     // The wildcard/wildcard event handler gets every event that does not have
  34.     // a specific event handler installed.
  35.     
  36.     if (success) 
  37.         success = (AEInstallEventHandler(typeWildCard, 
  38.                                                     typeWildCard, 
  39.                                                     NewAEEventHandlerProc(HandleEveryOtherAppleEvent), 
  40.                                                     0, 
  41.                                                     false) == noErr);
  42.                                                         
  43.     if (success) 
  44.         success = InstallRequiredSuiteHandlers();
  45.     
  46.     if (success) 
  47.         success = InstallCoreSuiteHandlers();
  48.             
  49.     if (success) 
  50.         success = InstallMiscSuiteHandlers();
  51.             
  52.     if (success) 
  53.         success = InstallObjectCallbackFunctions();
  54.     
  55.     if (success) 
  56.         success = InstallCoercionHandlers();
  57.  
  58.     return success;
  59. }
  60.  
  61. /*****************************************************************************
  62.  * 
  63.  * HasAppleEvents()
  64.  * 
  65.  * Does the system software support AppleEvents?
  66.  *
  67.  *****************************************************************************/
  68.  
  69. Boolean
  70. HasAppleEvents(void)
  71. {
  72.     Boolean hasIt = false;
  73.     long result = 0L;
  74.     
  75.     OSErr error = Gestalt(gestaltAppleEventsAttr, &result);
  76.     
  77.     hasIt = (result & (1 << gestaltAppleEventsPresent));
  78.     
  79.     return hasIt;
  80. }
  81.  
  82. //----------------------------------------------------------------------------------
  83. //    This is a wildcard/wildcard handler that receives Apple events which
  84. // do not have a specific handler installed.
  85. // This can be useful for debugging. You'll find out every event your application 
  86. // receives that you don't explicity handle, if you set a breakpoint in this function.
  87. //----------------------------------------------------------------------------------
  88.  
  89. pascal OSErr HandleEveryOtherAppleEvent(AppleEvent *appleEvent, AppleEvent *reply, long refCon)
  90. {
  91.     #pragma unused        (reply, refCon)
  92.     OSErr                    error            = noErr;
  93.     AEEventClass        eventClass;
  94.     AEEventID            eventID;
  95.     OSType                typeCode;
  96.     Size                    actualSize     = 0L;
  97.     
  98.     // Get the event class
  99.     
  100.     error = AEGetAttributePtr(appleEvent, 
  101.                                       keyEventClassAttr, 
  102.                                       typeType, 
  103.                                       &typeCode, 
  104.                                       (Ptr)&eventClass, 
  105.                                       sizeof(eventClass), 
  106.                                       &actualSize);
  107.     
  108.     // Get the event ID
  109.     
  110.     error = AEGetAttributePtr(appleEvent, 
  111.                                       keyEventIDAttr, 
  112.                                       typeType, 
  113.                                       &typeCode, 
  114.                                       (Ptr)&eventID, 
  115.                                       sizeof(eventID), 
  116.                                       &actualSize);
  117.  
  118.     // We ignore recording events for now
  119.  
  120.     switch (eventID)
  121.     {
  122.         case 'rec0':                // Recording turned off
  123.         case 'rec1':                // Recording turned on
  124.             return noErr;
  125.     }
  126.     
  127.     return errAEEventNotHandled; // to give system handlers a chance at the event
  128. }
  129.  
  130.  
  131. //----------------------------------------------------------------------------------
  132. //    Check to see if there exists any additional required parameters in the Apple Event.
  133. //    If so, return an error to the calling routine, because we didn't extract them all.
  134. //----------------------------------------------------------------------------------
  135.  
  136. OSErr 
  137. CheckForUnusedParameters(const AppleEvent* appleEvent)
  138. {
  139.     OSErr        error         = noErr;
  140.     
  141.     DescType    actualType    = typeNull;
  142.     Size        actualSize    = 0L;
  143.     
  144.     error = AEGetAttributePtr(appleEvent, 
  145.                                     keyMissedKeywordAttr, 
  146.                                     typeWildCard,
  147.                                     &actualType, 
  148.                                     nil, 
  149.                                     0, 
  150.                                     &actualSize);
  151.                                     
  152.     if (error == errAEDescNotFound)
  153.         error = noErr;
  154.     else
  155.         error = errAEParamMissed;
  156.         
  157.     return error;
  158. }
  159.  
  160. /*****************************************************************************
  161.  * 
  162.  * PutReplyErrorNumber
  163.  * 
  164.  * If a reply is expected, the error number is returned in the reply parameter.
  165.  * 
  166.  *****************************************************************************/
  167.  
  168. OSErr 
  169. PutReplyErrorNumber(AppleEvent* reply, long errorNumber)
  170. {
  171.     OSErr error = noErr;
  172.     
  173.     if (reply->dataHandle != nil && errorNumber != noErr)
  174.         error = AEPutParamPtr(reply, 
  175.                                      keyErrorNumber, 
  176.                                      typeLongInteger, 
  177.                                      (Ptr)&errorNumber, 
  178.                                      sizeof(long));
  179.     
  180.     return error;
  181. }
  182.  
  183. /*****************************************************************************
  184.  * 
  185.  * PutReplyErrorMessage
  186.  * 
  187.  * If a reply is expected, the error message is inserted into the reply parameter.
  188.  * 
  189.  *****************************************************************************/
  190.  
  191. OSErr 
  192. PutReplyErrorMessage(AppleEvent* reply, char *message)
  193. {
  194.     OSErr error = noErr;
  195.     
  196.     if (reply->dataHandle != nil && message != NULL)
  197.         error = AEPutParamPtr(reply, 
  198.                                      keyErrorString, 
  199.                                      typeChar, 
  200.                                      (Ptr)message, 
  201.                                      strlen(message));
  202.     
  203.     return error;
  204. }
  205.  
  206. // ------------------------------------------------------------------------
  207. // This is used to extract the type of object that an appleevent is supposed
  208. // to work with. In the Core events, this includes:
  209. //        count <reference> each <typeOfObject>
  210. //        make new <typeOfObject>
  211. //        save <reference> in <alias> as <typeOfObject>
  212. // ------------------------------------------------------------------------
  213.  
  214. OSErr
  215. GetObjectClassFromAppleEvent(const AppleEvent *appleEvent, DescType *objectClass)
  216. {
  217.     OSErr        error     = noErr;
  218.     OSType    typeCode;                    // should be typeType
  219.     long        actualSize;
  220.     
  221.     // Get the class of object that we will count
  222.  
  223.     error = AEGetParamPtr(appleEvent, 
  224.                                  keyAEObjectClass, 
  225.                                  typeType, 
  226.                                  &typeCode,
  227.                                  (Ptr)objectClass, 
  228.                                  sizeof(DescType), 
  229.                                  &actualSize);
  230.                                  
  231.     if (typeCode != typeType) 
  232.         error = errAECoercionFail;
  233.     
  234.     return error;
  235. }
  236.  
  237. // ------------------------------------------------------------------------
  238.  
  239.